

$(function () {
    let page= getUrl("pageid");
    let category=getUrl("categoryid");

    init()
    function init() {
        category_data();
        next()
        paging()
    }
 
    function category_data() {
       let url="http://193.112.55.79:9090/api/getproductlist?categoryid="+category+"&pageid="+page;
        $.ajax({
            type: "get",
            url: url,
            dataType: "json",
            success: function (response) {
                let data = response.result;

                let num=Math.ceil(response.totalCount/response.pagesize);
                let op="";
                for(let i=1;i<=num;i++){
                 
                  op+='<option value="'+ i+'">第'+ i+'页</option>'
                }
                $('#select').html(op);
              
                if(response.result.length==0){
                    page=page-1;//结束翻页
                   
                    mui.toast('没有数据',) 
                }else{
                    $('#select option').eq(page-1).attr("selected","selected");
                    let html = template('data_list', { arr: data })
                    $('#ylq_main_ul').html(html);
                }

            }
        });
    }

    function paging() {
        $("#bth_paging").on('tap', function () {
            console.log(page);
            if(!page==0){
                page--;
              
            }else{
                mui.toast('沒有上一页',) 
            }
            category_data();
        })
    }


    function next() {
        $('#bth_next').on('tap', function () {
            page++;  
            category_data();
        })
    }


    
    //下拉菜单

    $('#select').on('change', function () {
        
        console.log( $(this).val());
        $(this).attr("selected","selected")
        page=$(this).val();
        category_data()
  
      })


    function getUrl(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return decodeURI(r[2]);
        return null;
    }



})